package com.javaweb.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.javaweb.admin.entity.KdlPurchasein;
import com.javaweb.admin.entity.KdlPurchasewe;
import com.javaweb.admin.entity.KdlPurchaseweB;
import com.javaweb.admin.mapper.KdlPurchaseweBMapper;
import com.javaweb.admin.mapper.KdlPurchaseweMapper;
import com.javaweb.admin.query.KdlPurchaseinQuery;
import com.javaweb.admin.query.KdlPurchaseweQuery;
import com.javaweb.admin.service.IKdlPurchaseweService;
import com.javaweb.admin.vo.kdlpurchasein.KdlPurchaseinInfoVo;
import com.javaweb.common.common.BaseQuery;
import com.javaweb.common.common.BaseServiceImpl;
import com.javaweb.common.config.CommonConfig;
import com.javaweb.common.config.DataSourceType;
import com.javaweb.common.config.SpecifyDataSource;
import com.javaweb.common.utils.DateUtils;
import com.javaweb.common.utils.JsonResult;
import com.javaweb.common.utils.StringUtils;
import com.javaweb.system.utils.ShiroUtils;
import com.seegw.shaphar.IKDLService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

@Service
public class KdlPurchaseweServiceImpl extends BaseServiceImpl<KdlPurchaseweMapper, KdlPurchasewe> implements IKdlPurchaseweService {

    @Autowired
    private KdlPurchaseweMapper kdlPurchaseweMapper;
    @Autowired
    private KdlPurchaseweBMapper kdlPurchaseweBMapper;

    @Autowired
    private IKDLService kdlService;

    @Override
    @SpecifyDataSource(value = DataSourceType.SINESYS)
    public JsonResult getList(BaseQuery query) {
        KdlPurchaseweQuery kdlPurchaseinQuery = (KdlPurchaseweQuery) query;
        // 查询条件
        QueryWrapper<KdlPurchasewe> queryWrapper = new QueryWrapper<>();
        // queryWrapper.orderByDesc("id");
        if(!StringUtils.isEmpty(kdlPurchaseinQuery.getVendorpo())){
            queryWrapper.like("vendorpo",kdlPurchaseinQuery.getVendorpo());
        }
        if(!StringUtils.isEmpty(kdlPurchaseinQuery.getCahdocno())){
            queryWrapper.like("cahdocno",kdlPurchaseinQuery.getCahdocno());
        }
        if(!StringUtils.isEmpty(kdlPurchaseinQuery.getRiqiBegin())){
            queryWrapper.gt("grdate",kdlPurchaseinQuery.getRiqiBegin());
        }
        if(!StringUtils.isEmpty(kdlPurchaseinQuery.getRiqiEnd())){
            queryWrapper.lt("grdate",kdlPurchaseinQuery.getRiqiEnd());
        }


        // 获取数据列表
        IPage<KdlPurchasewe> page = new Page<>(kdlPurchaseinQuery.getPage(), kdlPurchaseinQuery.getLimit());
        IPage<KdlPurchasewe> pageData = kdlPurchaseweMapper.selectPage(page, queryWrapper);
//        pageData.convert(x -> {
//            KdlPurchaseinListVo kdlPurchaseinListVo = Convert.convert(KdlPurchaseinListVo.class, x);
//            return kdlPurchaseinListVo;
//        });
        return JsonResult.success(pageData);
    }



    @Override
    @SpecifyDataSource(value = DataSourceType.SINESYS)
    public Object getInfo(Serializable id) {
        KdlPurchasein entity = (KdlPurchasein) super.getInfo(id);
        // 返回视图Vo
        KdlPurchaseinInfoVo kdlPurchaseinInfoVo = new KdlPurchaseinInfoVo();
        // 拷贝属性
        BeanUtils.copyProperties(entity, kdlPurchaseinInfoVo);
        return kdlPurchaseinInfoVo;
    }



    @Override
    @SpecifyDataSource(value = DataSourceType.SINESYS)
    public JsonResult getPurchaseweList(BaseQuery query) {
        KdlPurchaseweQuery kdlPurchaseweQuery = (KdlPurchaseweQuery) query;
        // 获取数据列表
        kdlPurchaseweQuery.setOrglist(ShiroUtils.getOrgCode());

        IPage<KdlPurchasewe> page = new Page<>(kdlPurchaseweQuery.getPage(), kdlPurchaseweQuery.getLimit());
        IPage<KdlPurchasewe> pageData = kdlPurchaseweMapper.getPurchaseweList(page, kdlPurchaseweQuery);
        return JsonResult.success(pageData);
    }

    @Override
    @SpecifyDataSource(value = DataSourceType.SINESYS)
    public JsonResult getPruchaseweDetail(Integer pid) {
        return JsonResult.success(kdlPurchaseweMapper.getPurchaseweDetail(pid));
    }

    @Override
    @SpecifyDataSource(value = DataSourceType.SINESYS)
    public JsonResult syncPurchase(KdlPurchaseweQuery query) {
        if(StringUtils.isEmpty(query.getPrincipalid())){
            query.setPrincipalid(CommonConfig.kdlCompanyId);
        }
        query.setStarttime(kdlPurchaseweMapper.GetMaxSyncTime());
        query.setEndtime(DateUtils.getTime());
        return kdlService.syncPurchasewe(query);
    }
}
